import notify from 'devextreme/ui/notify';
import { FileDownloadService } from './../../file.download.service';
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-custom-file',
  templateUrl: './custom-file.component.html',
  styleUrls: ['./custom-file.component.css']
})
export class CustomFileComponent implements OnInit {
  @Input() text: string = '选择文件'; 
  file: any; // 文件对象 
  @Input() fileType: string[] | string;
  @Input() msg: string = '请上传对应类型文件';
  @Output() fileChange: EventEmitter<any> = new EventEmitter();
  fileName: string = '';

  _fileChange(event){
    if(event.target.files && event.target.files.length > 0){
      const file = event.target.files[0];
      if(!FileDownloadService.checkFileType(file.name, this.fileType)){
        notify(this.msg, 'error', 3000);
        this.file = null;
        return;
      };
      this.fileName = file.name;
      this.fileChange.emit({
        file
      })
    }
    
  }
  constructor() { }

  ngOnInit() {
  }

  clearFile(){
    this.file = null;
    this.fileName = '';
  }

  reset(){
    this.file = null;
    this.fileName = '';
  }
}
